package com.joysuch.wwyt.datadump.biz.processor;

import cn.zlg.common.datadump.annotation.DataDumpEntity;
import cn.zlg.common.datadump.processor.BaseImportEntityProcessor;
import com.joysuch.wwyt.bp.entity.contractor.BpContractor;
import com.joysuch.wwyt.bp.entity.contractor.dsl.QBpContractorStaff;
import com.joysuch.wwyt.bp.enums.DataStateTypes;
import com.joysuch.wwyt.bp.repository.BpContractorDao;
import com.joysuch.wwyt.core.enums.DeleteFlag;
import com.joysuch.wwyt.datadump.biz.model.BpContractorDumpBean;
import com.joysuch.wwyt.datadump.biz.model.MhMonitorPointDumpBean;
import com.joysuch.wwyt.mh.entity.MhMonitoringPoint;
import com.joysuch.wwyt.mh.entity.MhMonitoringType;
import com.joysuch.wwyt.mh.repository.MhMonitoringPointDao;
import com.joysuch.wwyt.mh.repository.MhMonitoringTypeDao;
import com.mysema.commons.lang.Assert;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class BpContractorImportProcessor extends BaseImportEntityProcessor<BpContractorDumpBean, BpContractor> {

    @Autowired
    private BpContractorDao bpContractorDao;

    /**
     * 保存后执行，确保新增、修改实体，entity都是持久化对象。
     * 如果是 processBeforeSave(), 创建的实体如果已经存在，将复制 DumpBean中importIgnore=false的属性，导致monitoringType属性丢失
     *
     * @param origin
     * @param entity
     * @param annotation
     * @return
     */
    @Override
    public BpContractor processBeforePersist(BpContractorDumpBean origin, BpContractor entity, DataDumpEntity annotation, boolean override) {
        entity.setState(DataStateTypes.EFFECTIVE.getState());
        if (entity.getId() != null) {
            if (bpContractorDao.countByNameAndIdNotEq(entity.getCompanyName(), entity.getId()) > 0) {
                throw new IllegalArgumentException("承包商单位名称已存在");
            }
        } else {
            if (bpContractorDao.countByName(entity.getCompanyName()) > 0) {
                throw new IllegalArgumentException("承包商单位名称已存在");
            }
        }
        return entity;
    }


}
